{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "from datasets import load_dataset\n",
    "\n",
    "ds = load_dataset(\"likaixin/TACO-verified\", split=\"train\", trust_remote_code=True)\n",
    "\n",
    "print(ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TACO dataset has difficulties as strings: EASY, MEDIUM, MEDIUM_HARD, HARD, VERY_HARD\n",
    "# Let EASY->1.9, MEDIUM->3.7, MEDIUM_HARD->5.5, HARD->7.3, VERY_HARD->9.1\n",
    "def difficulty_to_int(difficulty):\n",
    "    if difficulty == \"EASY\":\n",
    "        return 1.9\n",
    "    elif difficulty == \"MEDIUM\":\n",
    "        return 3.7\n",
    "    elif difficulty == \"MEDIUM_HARD\":\n",
    "        return 5.5\n",
    "    elif difficulty == \"HARD\":\n",
    "        return 7.3\n",
    "    elif difficulty == \"VERY_HARD\":\n",
    "        return 9.1\n",
    "    else:\n",
    "        # print(\"difficulty\", difficulty)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ast\n",
    "\n",
    "dataset = []\n",
    "unknown_difficulty = 0\n",
    "for entry in ds:\n",
    "    tests = entry[\"input_output\"]\n",
    "    if not tests:\n",
    "        continue\n",
    "\n",
    "    if isinstance(tests, str):\n",
    "        try:\n",
    "            tests = ast.literal_eval(entry[\"input_output\"])\n",
    "        except (ValueError, SyntaxError):\n",
    "            # Try Json loads instead\n",
    "            try:\n",
    "                tests = json.loads(entry[\"input_output\"])\n",
    "            except (json.JSONDecodeError, SyntaxError, ValueError) as e:\n",
    "                print(repr(entry[\"input_output\"]))\n",
    "                print(f\"Error in json.loads: {e}\")\n",
    "                continue\n",
    "    assert isinstance(tests, dict), \"Tests should be a dictionary\"\n",
    "    assert len(tests[\"inputs\"]) == len(tests[\"outputs\"]), \"Inputs and outputs should have the same length\"\n",
    "\n",
    "    new_entry = {\n",
    "        \"problem\": entry[\"question\"],\n",
    "        \"tests\": tests,\n",
    "        \"solutions\": entry[\"solutions\"],\n",
    "    }\n",
    "    # Assert tests is a dictionary of inputs and outputs that map to a list of strings\n",
    "    assert isinstance(tests, dict), \"Tests should be a dictionary\"\n",
    "    assert \"inputs\" in tests, \"Inputs should be a key in the dictionary\"\n",
    "    assert \"outputs\" in tests, \"Outputs should be a key in the dictionary\"\n",
    "    assert isinstance(tests[\"inputs\"], list), \"Inputs should be a list\"\n",
    "    assert isinstance(tests[\"outputs\"], list), \"Outputs should be a list\"\n",
    "    assert len(tests[\"inputs\"]) == len(tests[\"outputs\"]), \"Inputs and outputs should have the same length\"\n",
    "\n",
    "    inputs = tests[\"inputs\"]\n",
    "    outputs = tests[\"outputs\"]\n",
    "    if len(inputs) <= 4:\n",
    "        continue\n",
    "    dataset.append(new_entry)\n",
    "\n",
    "print(len(dataset))\n",
    "print(dataset[0])\n",
    "print(unknown_difficulty)\n",
    "\n",
    "output_dir = os.path.abspath(\"../../train/code\")\n",
    "output_file = os.path.join(output_dir, \"taco.json\")\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
